﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Shoozla.ViewModel;

namespace Shoozla.View
{
    /// <summary>
    /// Interaction logic for ProgressBarView.xaml
    /// </summary>
    public partial class ProgressBarView : Window
    {
        public ProgressBarView(int max,string action):base()
        {
            InitializeComponent();
            statusPB.Maximum = max;
            statusPB.Minimum = 0;
            statusTB.Text = action;
            countTB.Text = statusPB.Minimum + "/" + statusPB.Maximum;
            if (max == 0)
                StopProgressBar();
        }

        public void IncrementBar()
        {
            this.Dispatcher.Invoke((Action)(() =>
               {
                   statusPB.Value++;
                   countTB.Text = statusPB.Value +"/" + statusPB.Maximum;
                   if (statusPB.Value >= statusPB.Maximum)
                       StopProgressBar();
               })
           );
        }

        public void StopProgressBar()
        {
            this.Dispatcher.BeginInvoke((Action)(() =>
            {
                Visibility = System.Windows.Visibility.Hidden;
                this.Close();              
            })
            );
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            App.AppStatus a = App.CurrenAppStatus;

            switch (App.CurrenAppStatus)
            {
                case App.AppStatus.ApplyingCovers: { if (App.CoverWorker != null) App.CoverWorker.StopThreads(); break; }
                case App.AppStatus.SearchingCovers: { if (App.SearchWorker != null) App.SearchWorker.StopThreads(); break; }
                case App.AppStatus.ReadingFiles: { if (App.FileWorker != null) App.FileWorker.StopThreads(); break; }
                default:
                    {
                        if (App.CoverWorker != null) App.CoverWorker.StopThreads();
                        if (App.SearchWorker != null) App.SearchWorker.StopThreads();
                        if (App.FileWorker != null) App.FileWorker.StopThreads();
                        break;
                    }
            }
        }
    }
}
